package com.hhit.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.hhit.pojo.Item;
import com.hhit.service.ItemService;



@Controller
@RequestMapping("/item")
public class ItemController {

	@Resource
	private ItemService itemService;

	@RequestMapping("toItems")
	public String toUsers(ModelMap map) {
		List<Item> userList = itemService.getAll();
		map.put("itemList", userList);

		return "item/items";
	}
	
	@RequestMapping("toItemlist")
	public String toItemlist(ModelMap map) {
		List<Item> userList = itemService.getAll();
		map.put("itemList", userList);

		return "item/itemlist";
	}

	@RequestMapping("del")
	public String del(Integer id, HttpServletRequest req) {
		itemService.delItem(id, req);
		return "redirect:/item/toItems.do";
	}

	@RequestMapping("add")
	public String add(Item item,
			@RequestParam("myfile") CommonsMultipartFile myfile, HttpServletRequest req) {
		if (item.getId() > 0) {
			itemService.updateItem(item,myfile,req);
		} else {
			itemService.addItem(item,myfile,req);
		}

		return "redirect:/item/toItems.do";
	}

	@RequestMapping("toModify")
	public String toModify(Integer id, ModelMap map) {
		Item user = itemService.getItem(id);
		map.put("item", user);
		return "item/itemadd";
	}
}
